import numpy as np
import os
import msvcrt

def is_number(s): #判断字符是否为数字
    try:
        float(s)
        return True
    except ValueError:
        pass
 
    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass
 
    return False

# map = np.array([[1,1,1,1,1,1,1,1],
#                 [1,0,0,0,0,0,0,1],
#                 [1,1,1,1,1,1,0,1],
#                 [1,0,0,0,0,0,0,1],
#                 [1,0,1,1,1,1,1,1],
#                 [1,0,0,0,0,0,3,1],
#                 [1,1,1,1,1,1,1,1],
#                 [1,0,1,1,1,1,2,1],
#                 [1,0,0,0,0,0,0,1],
#                 [1,1,1,1,1,1,1,1]])
map = np.zeros([10,10])
#行和列的长度
lie = 0#10
hang = 0#10
#机关位置
jiguanXY = [6, 5]
#机关打开的位置
openXY = [1,6]
#终点位置
winX = 6
winY = 7
#玩家初始位置 左上角是(0,0)
playerX = 1
playerY = 1

#存档读取=======================(0空气;1墙;2终点;3机关位置;4机关打开的位置;5玩家初始位置;第一行为地图列数，第二行为地图行数)
file = input("请输入存档名(存档需在同目录下，需带上拓展名): ")
x = 0
f = open(file)
line = f.readline()
while line: 
    if(x == 0):
        lie = int(line) + 2
    elif(x == 1):
        hang = int(line)
        map = np.zeros([hang,lie])
    else:
        for b in range(len(line)):
            if(is_number(line[b])):
                if(int(line[b]) == 3):
                    jiguanXY[0] = b
                    jiguanXY[1] = x - 2
                elif(int(line[b]) == 4):
                    openXY[0] = b
                    openXY[1] = x - 2
                elif(int(line[b]) == 2):
                    winX = b
                    winY = x - 2
                elif(int(line[b]) == 5):
                    playerX = b
                    playerY = x - 2
                
                if(int(line[b]) == 4):
                    map[x-2, b] = 1
                elif(int(line[b]) == 5):
                    map[x-2,b] = 0
                else:
                    map[x-2, b] = int(line[b])
            else:
                pass
    x+=1
    line = f.readline() 
f.close()
#==============================

def drawMap(map,playerX,playerY):
    os.system("cls")
    for i in range(0,hang):
        for j in range(0,lie):
            if playerY == i and playerX == j:
                # print("O  ",end="")
                print("\033[0;;42m  \033[0m",end="  ")
            elif map[i,j] == 1:
                # print("■ ",end="")
                print("\033[0;;47m  \033[0m",end="  ")
            elif map[i,j] == 0:
                # print("   ",end="")
                print("\033[0;;40m  \033[0m",end="  ")
            elif map[i,j] == 2:
                # print("W  ",end="")
                print("\033[0;;43m  \033[0m",end="  ")
            elif map[i,j] == 3:
                #print("B  ",end="")
                print("\033[0;;44m  \033[0m",end="  ")
        print("\n")

    # print("\033[0;;47m  \033[0m",end="") #白块
    # print("\033[0;;40m  \033[0m",end="") #黑块
    # print("\033[0;;42m  \033[0m",end="") #绿块
    # print("\033[0;;44m  \033[0m",end="") #蓝块
    # print("\033[0;;43m  \033[0m",end="") #黄块

drawMap(map,playerX,playerY)
while True:
    getkey = msvcrt.getch().decode('ASCII')
    if getkey == "w":
        if map[playerY-1,playerX] == 1:
            # print(map[(playerY - 2) * 6 + playerX])
            # print((playerY - 2) * 6 + playerX)
            # for a in map:
            #     print(a,end=" ")
            pass
        else:
            playerY -= 1
            drawMap(map,playerX,playerY)
    if getkey == "s":
        if map[playerY+1,playerX] == 1:
            pass
        else:
            playerY += 1
            drawMap(map,playerX,playerY)
    if getkey == "a":
        if map[playerY,playerX-1] == 1:
            pass
        else:
            playerX -= 1
            drawMap(map,playerX,playerY)
    if getkey == "d":
        if map[playerY,playerX+1] == 1:
            pass
        else:
            playerX += 1
            drawMap(map,playerX,playerY)

    if map[playerY,playerX] == 3:
            map[openXY[1],openXY[0]] = 0
            drawMap(map,playerX,playerY)

    if playerX == winX and playerY == winY:
        print("You win!")
        os.system("pause")
        exit()